package com.bidevalution.business.common;

import com.qtp.core.util.GsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;

/**
 * 取路径工具包
 * @author lance 2017-12-20
 */
public class AnalysisFileUtils {
    private AnalysisFileUtils(){}
    private static Logger log = LoggerFactory.getLogger(AnalysisFileUtils.class);

    /**
     * 删除文件
     * @param sPath path
     */
    public static void delFile(String sPath) {
        delFile(new File(sPath));
    }

    /**
     * 删除文件
     * @param file file
     */
    public static void delFile(File file) {
        try {
            if (file.isDirectory()) {
                for (File f:
                     file.listFiles()) {
                    delFile(f);
                }
            }
            if(file.exists()){
                Files.delete(file.toPath());
            }
        } catch (Exception e) {
            log.error("LOG[]delFile[]", e);
        }
    }

    /**
     * 创建目录
     * @param filePath filePath
     * @return File
     */
    public static File mkDir(String filePath){
        File file = new File(filePath);
        file.mkdirs();
        AnalysisFileUtils.delFile(file);
        return file;
    }

    /**
     * 创建文件
     * @param filePath
     * @return
     */
    public static boolean createFile(String filePath) {
        File file = new File(filePath);
        if(file.exists()) {
            return true;
        }
        if (filePath.endsWith(File.separator)) {
            return false;
        }
        AnalysisFileUtils.mkDir(filePath);
        //创建目标文件
        try {
            return file.createNewFile();
        } catch (IOException e) {
            log.error("LOG[]createFile_265[]{},error={}","异常日志", GsonUtil.toJsonWithGson(e));
            System.out.println("创建单个文件" + filePath + "失败！" + e.getMessage());
            return false;
        }
    }
}